package com.cloud.auth.controller;

import com.cloud.auth.service.BaseService;
import com.cloud.auth.service.RolePermissionService;
import com.cloud.auth.util.ApiResult;
import com.cloud.auth.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @packageName: com.api.controller.auth
 * @name: RolePermissionController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/6/19 15:25
 */
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController extends BaseController {

    @Autowired
    RolePermissionService service;

    @Override
    protected BaseService getService() {
        return service;
    }

    /**
     * 角色配置权限
     *
     * @param map
     * @return
     */
    @PostMapping("/add")
    public ApiResult add(@RequestParam Map<String, Object> map) {
        String roleId = MapUtil.getString(map, "roleId");
        if (roleId == null) {
            return ApiResult.failResult(401, "角色ID不能为空");
        }
        int i = service.add(map);
        if (i > 0) {
            return ApiResult.successResult();
        }
        return ApiResult.failResult("配置权限失败");
    }

    /**
     * 角色权限列表
     *
     * @param map
     * @return
     */
    @PostMapping("/list")
    public ApiResult list(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "roleId") == null) {
            return ApiResult.failResult(401, "角色ID不能为空");
        }
        List<Map<String, Object>> list = service.simpleList(map);
        List<String> checkedId = service.ownPermission(map);
        List<Map<String, Object>> data = getChildren(list, checkedId, "-1");

        return ApiResult.successResultForData(data);
    }


    private List<Map<String, Object>> getChildren(List<Map<String, Object>> list, List<String> checkedIds, String fid) {
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
        for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = null;
            Map<String, Object> treeChild = list.get(j);
            String parentId = treeChild.get("parentId").toString();
            if (parentId.equals(fid)) {
                map = new HashMap<>();
                String id = treeChild.get("id").toString();
                map.put("id", id);
                map.put("title", treeChild.get("name"));
                boolean ownChildren = (long) treeChild.get("ownChildren") > 0;
                map.put("spread", ownChildren);
                boolean checked = checkedIds.contains(id);
                if ((!ownChildren) && checked) {
                    map.put("checked", true);
                }
                map.put("children", getChildren(list, checkedIds, id));
            }
            if (map != null) {
                childList.add(map);
            }
        }
        return childList;
    }

}
